package com.kantboot.global.handler;

import com.kantboot.system.dict.domain.dto.ToGobleDTO;
import com.kantboot.system.dict.service.ISysDictI18nService;
import com.kantboot.util.common.http.HttpRequestHeaderUtil;
import com.kantboot.util.common.result.RestResult;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@ControllerAdvice
public class GlobeResponseBodyAdviceAdapter implements ResponseBodyAdvice<Object> {

    @Resource
    private HttpRequestHeaderUtil httpRequestHeaderUtil;

    @Resource
    private ISysDictI18nService sysDictI18nService;

    @Value("${isChineseMainland}")
    private Boolean isChineseMainland;

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        String languageCode = httpRequestHeaderUtil.getLanguageCode();

        if (body instanceof RestResult<?>) {
            RestResult<?> result = (RestResult<?>) body;
            if(languageCode==null||languageCode.equals("zh_CN")){
                return result;
            }
            if(isChineseMainland){
                return result;
            }
            try{
                if(!result.isSuccess()){
                    ToGobleDTO toGobleDTO = new ToGobleDTO();
                    toGobleDTO.setDictGroupCode("requestException");
                    toGobleDTO.setDictCode(result.getMsgDictCode());
                    toGobleDTO.setSourceLanguageCode("zh-CN");
                    toGobleDTO.setTargetLanguageCode(languageCode);
                    toGobleDTO.setText(result.getErrMsg());
                    String text = sysDictI18nService.toTargetLanguageCode(toGobleDTO);
                    result.setErrMsg(text);
                }else{
                    ToGobleDTO toGobleDTO = new ToGobleDTO();
                    toGobleDTO.setDictGroupCode("requestSuccess");
                    toGobleDTO.setDictCode(result.getMsgDictCode());
                    toGobleDTO.setSourceLanguageCode("zh-CN");
                    toGobleDTO.setTargetLanguageCode(languageCode);
                    toGobleDTO.setText(result.getMsg());
                    result.setMsg(sysDictI18nService.toTargetLanguageCode(toGobleDTO));
                }
            }catch (Exception e){
                System.err.println(e.getMessage());
                e.printStackTrace();
                return result;
            }


            return result;
        }
        return body;
    }

}